package com.erp.app.admin.companyMenu.web;

import java.util.Hashtable;
import java.util.ArrayList;

//JRE Classes
import javax.servlet.http.HttpServletRequest;

import org.apache.struts.action.ActionErrors;
import org.apache.struts.action.ActionMapping;

import com.erp.framework.controller.form.SimpleSearchStyleForm;
import com.erp.framework.util.logger.Logger;
import com.erp.framework.util.CommonUtil;

import com.erp.app.common.ProjectConstants;
import com.erp.app.common.select.beans.SelectDelegate;

import com.erp.app.setup.codeSetup.web.CodeDtlRow;


public class CompanyMenuSearchForm extends SimpleSearchStyleForm {
    /*
     * Begin: will be auto-reseted when action=clear
     */
    private String srchMenuName;
    private String srchMenuId;
    private String srchMenuType;
    private String srchParentMenuId;
    private String srchGroupId;
    private String srchRoleGroupId;

    /*
     * End: will be auto-reseted when action=clear
     */

    private String editCompanyCode;
    private String editGroupId;
    private String editMenuId;
    private String editRoleGroupId;
    private String editExistInd;

    public void otherReset(ActionMapping mapping, HttpServletRequest request) {
        String emptyString = "";
        editCompanyCode = emptyString;
        editGroupId = emptyString;
        editMenuId = emptyString;
        editRoleGroupId = emptyString;
        editExistInd = emptyString;
    }

    public void otherValidate(ActionErrors errors) {
        // if (CommonUtil.isEmpty(srchRoleGroupId)) {
        // errors.add(ActionErrors.GLOBAL_ERROR, new
        // ActionError("error.general", "Must enter Role Group ID"));
        // }
    }

    public String getSrchMenuName() {
        return srchMenuName;
    }

    public void setSrchMenuName(String srchMenuName) {
        this.srchMenuName = srchMenuName;
    }

    public String getSrchMenuType() {
        return srchMenuType;
    }

    public void setSrchMenuType(String srchMenuType) {
        this.srchMenuType = srchMenuType;
    }

    public String getSrchParentMenuId() {
        return srchParentMenuId;
    }

    public void setSrchParentMenuId(String srchParentMenuId) {
        this.srchParentMenuId = srchParentMenuId;
    }

    public String getEditCompanyCode() {
        return editCompanyCode;
    }

    public void setEditCompanyCode(String editCompanyCode) {
        this.editCompanyCode = editCompanyCode;
    }

    public String getEditGroupId() {
        return editGroupId;
    }

    public void setEditGroupId(String editGroupId) {
        this.editGroupId = editGroupId;
    }

    public String getEditMenuId() {
        return editMenuId;
    }

    public void setEditMenuId(String editMenuId) {
        this.editMenuId = editMenuId;
    }

    /**
     * @return
     */
    public String getSrchGroupId() {
        return srchGroupId;
    }

    /**
     * @param string
     */
    public void setSrchGroupId(String string) {
        srchGroupId = string;
    }

    /**
     * @return
     */
    public String getSrchMenuId() {
        return srchMenuId;
    }

    /**
     * @param string
     */
    public void setSrchMenuId(String string) {
        srchMenuId = string;
    }

    public String getSrchRoleGroupId() {

        return srchRoleGroupId;
    }

    public void setSrchRoleGroupId(String srchRoleGroupId) {

        this.srchRoleGroupId = srchRoleGroupId;
    }

    public String getEditRoleGroupId() {

        return editRoleGroupId;
    }

    public void setEditRoleGroupId(String editRoleGroupId) {

        this.editRoleGroupId = editRoleGroupId;
    }

    public String getEditExistInd() {

        return editExistInd;
    }

    public void setEditExistInd(String editExistInd) {

        this.editExistInd = editExistInd;
    }

        public ArrayList  getCodeDtlDropdownCollection(String shareCompanyCode, 
                                                   String  refMasterCode, 
                                                   String  columnValue, 
                                                   boolean  isCriteriaSearch){ 

        ArrayList collection = new  ArrayList();

        SelectDelegate  selectDelegate = new SelectDelegate();   
        String whereClause =  " hdr.REF_MASTER_CODE='" + refMasterCode + "'  " 
                           + " AND hdr.COMPANY_CODE = '" +  shareCompanyCode + "' " 
                           + " AND  hdr.COMPANY_CODE = dtl.COMPANY_CODE " 
                           +  " AND hdr.REF_MASTER_CODE = dtl.REF_MASTER_CODE  " 
                           + (isCriteriaSearch?"":(" AND  (dtl.REF_STATUS = '"+ProjectConstants.GENERIC_ACTIVE+"' OR dtl.REF_CODE = '" +  columnValue + "')")); 
        String codeDesc = "  decode(nvl(hdr.SHOW_DESC, '"+ProjectConstants.GENERIC_YES+"'),  '"+ProjectConstants.GENERIC_YES+"', dtl.REF_DESC, dtl.REF_CODE)"; 
  
        String  srchTableName = "T_CODE_DTL dtl, T_CODE_HDR hdr"; 
  
        String[]  srchColumn = new String[2]; 
        srchColumn[0] = " dtl.REF_CODE AS CODE  "; 
        srchColumn[1] = codeDesc + " AS  DESCRIPTION "; 
  
        ArrayList result = new ArrayList();
        Hashtable row =  new Hashtable(); 
        result = selectDelegate.findFieldList( srchTableName, srchColumn,  whereClause); 
        
        CodeDtlRow cdr =  new CodeDtlRow(); 
        for (int i = 0 ;  result.size() > 0 && i < result.size() ; i++  ){ 
            row =  (Hashtable)result.get(i); 
            cdr = new  CodeDtlRow(); 
              
            cdr.setCompanyCode(shareCompanyCode); 
            cdr.setRefMasterCode(refMasterCode); 
            cdr.setRefCode(CommonUtil.nullToString(row.get("CODE"))); 
            cdr.setDtlRefDesc(CommonUtil.nullToString(row.get("DESCRIPTION"))); 
  
            collection.add(cdr); 
         }  
        return  collection; 
    } 

}
